package com.joy.web.common.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.joy.common.exception.JsonException;
import com.joy.common.model.vo.FileInfoVO;
import com.joy.conf.properties.FilePathProperties;
import com.joy.common.enums.Status;
import com.joy.web.base.service.impl.IBaseServiceImpl;
import com.joy.web.common.entity.FileInfo;
import com.joy.web.common.mapper.FileInfoMapper;
import com.joy.web.common.service.IFileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

/**
 * <p>
 * 文件信息表 服务实现类
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-20
 */
@Slf4j
@Service
public class FileInfoServiceImpl extends IBaseServiceImpl<FileInfoMapper, FileInfo> implements IFileInfoService {

    @Autowired
    FilePathProperties filePathProperties;

    @Override
    public Optional<FileInfoVO> localFileUp(MultipartFile uploadFile, String sourceType) {

        //文件名包括文件扩展名
        String fileAllNm = uploadFile.getOriginalFilename();
        //文件名
        String fileNm = StrUtil.subBefore(fileAllNm, StrUtil.DOT, true);
        //文件格式
        String fileFormat = StrUtil.subAfter(fileAllNm, StrUtil.DOT, true);
        //文件存储目录
        String fileDirPath = new StrBuilder(StrUtil.SLASH)
                .append(sourceType)
                .append(StrUtil.SLASH)
                .append(DateUtil.today()).toString();

        //文件全路径
        String subFilePath = new StrBuilder(fileDirPath)
                .append(StrUtil.SLASH)
                .append(fileNm)
                .append(StrUtil.DASHED)
                .append(DateUtil.current(false))
                .append(StrUtil.DOT)
                .append(fileFormat).toString();

        //文件本地存放路径
        String localFilePath =  new StrBuilder(filePathProperties.getDir())
                .append(subFilePath)
                .toString();
        try {
            // 获取文件存放 文件加目录
            String fileAllDirPath = new StringBuffer(filePathProperties.getDir())
                    .append(fileDirPath).toString();
            if (!FileUtil.exist(fileAllDirPath)) {
                FileUtil.mkdir(fileAllDirPath);
            }
            uploadFile.transferTo(new File(localFilePath));
        } catch (IOException e) {
            log.error("localFileUp transferTo error localFilePath:{}", localFilePath);
            e.printStackTrace();
            throw new JsonException(Status.UPLOAD_ERROR);
        }
        //文件展示路径
        String showUriPath =  new StrBuilder(StrUtil.SLASH)
                .append(filePathProperties.getPath())
                .append(subFilePath)
                .toString();

        //保存文件信息
        FileInfo fileInfo = new FileInfo();
        fileInfo.setFileNm(fileNm);
        fileInfo.setFileFormat(fileFormat);
        fileInfo.setFileLocation(localFilePath);
        fileInfo.setFileShowUri(showUriPath);
        fileInfo.setFileSourceType(sourceType);
        boolean save = this.save(fileInfo);
        FileInfoVO fileInfoVO = fileInfo.convert(FileInfoVO.class);
        log.error("localFileUp succ localFilePath:{}, showUriPath:{}", localFilePath, showUriPath);
        return Optional.of(fileInfoVO);
    }
}
